package com.qianfeng.test.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.internal.LoadingLayout;
import com.qianfeng.test.GridViewActivity;
import com.qianfeng.test.R;
import com.qianfeng.test.adapter.GridViewAdapter;
import com.qianfeng.test.adapter.MainItemAdapter;
import com.qianfeng.test.bean.ShopInfo;
import com.qianfeng.test.bean.gridview_bean.MenuMain;
import com.qianfeng.test.bean.gridview_bean.NameInfo;
import com.qianfeng.test.constants.UrlConstans;
import com.qianfeng.test.task.GridViewTask;
import com.qianfeng.test.task.MainTask;
import com.qianfeng.test.task.ViewPagerTask;
import com.qianfeng.test.utils.FragmentTabUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 15-7-21.
 */
public class GroupBuyFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener {
    private PullToRefreshListView ptrListView;
    private ArrayList<ShopInfo> list = new ArrayList<ShopInfo>();
 //   private List<NameInfo> gridlist = new ArrayList<NameInfo>();
    private MainItemAdapter mainItemAdapter;
    private GridViewAdapter gridAdapter;
    private int page = 1;
    private ILoadingLayout loadingLayout;
    private boolean isRefresh = false;
    private ViewPager viewPager;
    private View[] views;
    private  MyGridView myGridView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_group_buy, container, false);

        initView(view);
        initData();//
        return view;
    }
    //初始化数据
    private void initData() {
        System.out.print("GroupBuyFragment....进来了");
        new MainTask(mainItemAdapter, list, ptrListView, page).execute(UrlConstans.getShopUrl(page));
        //new ViewPagerTask(views,getActivity()).execute(UrlConstans.Url_APPTOP_ADD+UrlConstans.Url_APPTOP);
        //设置滑动模式
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        //设置滑动监听事件
        ptrListView.setOnRefreshListener(this);
        ptrListView.setRefreshing(); //
        //设置ptrListView每个item的点击事件
       // ptrListView.setOnItemClickListener(this);
        // new GridViewTask(gridAdapter,gridlist).execute(UrlConstans.Url_GRIDVIEW);
        //new
    }
    //初始化视图
    private void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.main_ptrlistview);

        ListView listView = ptrListView.getRefreshableView();//什么意思
        View headview = getActivity().getLayoutInflater().inflate(R.layout.main_head_layout,null);
        viewPager = (ViewPager) headview.findViewById(R.id.head_viewPager);//
        myGridView = (MyGridView) headview.findViewById(R.id.head_gridView);//

        mainItemAdapter = new MainItemAdapter(list, getActivity());
        ptrListView.setAdapter(mainItemAdapter);//错误
        loadingLayout = ptrListView.getLoadingLayoutProxy();//可以得到一个ILoadingLayout 对象，这个对象可以设置各种指示器中的样式，文本等。

//        ILoadingLayout headLayout = ptrListView.getLoadingLayoutProxy(true,false);
//        ILoadingLayout footLayout = ptrListView.getLoadingLayoutProxy(false,true);

        //给Viewpager添加布局
        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view1 = inflater.inflate(R.layout.head_img1_layout, null);
        View view2 = inflater.inflate(R.layout.head_img2_layout, null);
        views = new View[]{view1, view2};
        viewPager.setAdapter(new MyAdapter());//

        //添加gridView
        myGridView = (MyGridView) headview.findViewById(R.id.head_gridView);
        List<MenuMain> gridlist = new ArrayList<MenuMain>();
        gridlist.add(new MenuMain("游泳票",R.mipmap.ic_category_0));
        gridlist.add(new MenuMain("代金券", R.mipmap.ic_category_1));
        gridlist.add(new MenuMain("中餐", R.mipmap.ic_category_2));
        gridlist.add(new MenuMain("西餐", R.mipmap.ic_category_3));
        gridlist.add(new MenuMain("特色餐饮", R.mipmap.ic_category_4));
        gridlist.add(new MenuMain("日韩料理", R.mipmap.ic_category_5));
        gridlist.add(new MenuMain("火锅", R.mipmap.ic_category_6));
        gridlist.add(new MenuMain("全部分类", R.mipmap.ic_category_7));
       // gridView.setAdapter(new GridViewMyAdapter(gridlist));
        gridAdapter = new GridViewAdapter(gridlist,getActivity());
        myGridView.setOnItemClickListener(this);//监听事件没有响应
        myGridView.setAdapter(gridAdapter);
        //给ptrListView添加头部
        listView.addHeaderView(headview);

    }
    //ptrListView滑动事件
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadingLayout.setLastUpdatedLabel("最后更新时间");
        initData();
        isRefresh = true;
    }
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        page++;
        initData();
    }

    //listview和gridview的item点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 1:
                Intent intent1 = new Intent(getActivity(), GridViewActivity.class);
                intent1.putExtra("type","105");
                startActivity(intent1);
                break;
            case 2:
                Intent intent2 = new Intent(getActivity(), GridViewActivity.class);
                intent2.putExtra("type","40");
                startActivity(intent2);
                break;
            case 3:
                Intent intent3 = new Intent(getActivity(), GridViewActivity.class);
                intent3.putExtra("type","32");
                startActivity(intent3);
                break;

        }
    }
        //设置ViewPage（图片）的适配器
        class MyAdapter extends PagerAdapter {
            private String tag = "MyAdapter";

            @Override
            public int getCount() {
                Log.e(tag, "getCount....");
                // TODO Auto-generated method stub
                return views.length;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                Log.e(tag, "isViewFromObject...." + (arg0 == arg1));
                return arg0 == arg1;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                Log.e(tag, "instantiateItem...." + position);
                container.addView(views[position]);//把pager页面要显示的视图添加到ViewPager中
                return views[position];
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // TODO Auto-generated method stub
                Log.e(tag, "destroyItem...." + position);
                container.removeView((View) object);

            }

        }

    }





